package serve;

import BaseQuery.BaseQuery;
import Bean.Employee;
import Bean.PageBean;
import dao.EmployeeDao;

import java.util.List;

public class EmployeeServe implements BaseServe<Employee>{
	private EmployeeDao employeeDao = null;

	public EmployeeServe() {
		employeeDao = new EmployeeDao();
	}
	@Override
	public boolean add(Employee employee) throws Exception {
		if (employee == null) {
			return false;
		}
		if(employee.getEname()==null||"".equals(employee.getEname())) {
			return false;
		}
		int count = employeeDao.add(employee);
		return count > 0;
	}

	@Override
	public boolean update(Employee employee) throws Exception {
		if(employee==null) {
			return false;
		}
		if(employee.getEmpno()==null||employee.getEname()==null||"".equals(employee.getEname())) {
			return false;
		}
		int count=employeeDao.update(employee);
		return count>0;
	}

	@Override
	public boolean delete(Integer id) throws Exception {
		if(id==null){
			return false;
		}
		return employeeDao.delete(id)>0;
	}

	@Override
	public Employee getById(Integer id) throws Exception {
		if(id==null) {
			return null;
		}
		Employee employee=employeeDao.getById(id);
		return employee;
	}

	@Override
	public List getAll() throws Exception {
		return null;
	}

	@Override
	public PageBean getByPage(BaseQuery baseQuery) throws Exception {
		if(baseQuery==null) {
			baseQuery=new BaseQuery();
		}
		//计算总条数
		Integer totalRows =employeeDao.getCount();
		Integer totalPages=null;
		//计算页数
		if(totalRows% baseQuery.getPageCount()==0) {
			totalPages=totalRows/ baseQuery.getPageCount();
		} else {
			totalPages=totalRows/ baseQuery.getPageCount()+1;
		}

		List<Employee> empList = employeeDao.getByPage(baseQuery);
		//通过创建一个对象，来将数据全部打包给返回去
		PageBean<Employee> pageBean = new PageBean<>();
		pageBean.setTotalRows(totalRows);
		pageBean.setTotalPages(totalPages);
		pageBean.setData(empList);

		return pageBean;
	}
}
